﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Diagnostics;

namespace Lognet8
{


    public class HardwareInfo
    {
        public static string GetCpuInfo()
        {
            try
            {
                var process = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "bash",
                        Arguments = "-c \"lscpu | grep 'Model name' | cut -d':' -f2 | sed 's/^[ \\t]*//'\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
                process.Start();
                string cpuInfo = process.StandardOutput.ReadToEnd().Trim();
                process.WaitForExit();

                return string.IsNullOrEmpty(cpuInfo) ? "Unknown CPU" : cpuInfo;
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }

        public static string GetCpuId()
        {
            try
            {
                // 尝试获取更唯一的CPU标识
                var process = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "bash",
                        Arguments = "-c \"cat /proc/cpuinfo | grep 'vendor_id' | head -1 | cut -d':' -f2 | sed 's/^[ \\t]*//'\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
                process.Start();
                string vendorId = process.StandardOutput.ReadToEnd().Trim();

                // 获取CPU特性作为补充标识
                process.StartInfo.Arguments = "-c \"cat /proc/cpuinfo | grep 'cpu family' | head -1 | cut -d':' -f2 | sed 's/^[ \\t]*//'\"";
                process.Start();
                string cpuFamily = process.StandardOutput.ReadToEnd().Trim();

                process.WaitForExit();

                return $"{vendorId}-{cpuFamily}";
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }

        public static string GetMotherboardSerial()
        {
            try
            {
                // 获取主板序列号（需要root权限）
                var process = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "bash",
                        Arguments = "-c \"sudo dmidecode -s baseboard-serial-number 2>/dev/null || echo 'Permission denied or not available'\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
                process.Start();
                string serial = process.StandardOutput.ReadToEnd().Trim();
                process.WaitForExit();

                return string.IsNullOrEmpty(serial) ? "Unknown" : serial;
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }

        public static string GetMachineId()
        {
            try
            {
                // 获取系统机器ID（通常更可靠）
                var process = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "bash",
                        Arguments = "-c \"cat /etc/machine-id 2>/dev/null || cat /var/lib/dbus/machine-id 2>/dev/null || echo 'Not available'\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
                process.Start();
                string machineId = process.StandardOutput.ReadToEnd().Trim();
                process.WaitForExit();

                return machineId;
            }
            catch (Exception ex)
            {
                return $"Error: {ex.Message}";
            }
        }
    }
}
